/***********************************************************
*discription    按钮类、单选框、多选框示例
*author         david
*date           2021-5-12
*modify         2021-5-12 8：30
************************************************************/
#include "mainWindow.h"

mainWindow::mainWindow()
{
	//QRect screenRect = QApplication::desktop()->screenGeometry();
	//this.setGeometry(100, 100, 800, 400);
    setWindowTitle("按钮类示例");
    //按钮控件
    pushBtn1 = new QPushButton("pushBtn1");
    pushBtn2 = new QPushButton("pushBtn2");
    //策略
    QSizePolicy sizePolicy = this->sizePolicy();
    sizePolicy.setHorizontalPolicy(QSizePolicy::Expanding);
    sizePolicy.setVerticalPolicy(QSizePolicy::Expanding);
    pushBtn1->setSizePolicy(sizePolicy);
    pushBtn2->setSizePolicy(sizePolicy);
    //设置id
    pushBtn1->setProperty("btnID", 1);
    pushBtn2->setProperty("btnID", 2);

    /***********************多选****************************/
    checkBox1 = new QCheckBox("读书");
    checkBox2 = new QCheckBox("写字");
    //设置三态
    checkBox1->setTristate();
    //按钮组
    checkGroup = new QButtonGroup(this);
    //设置不互斥
    checkGroup->setExclusive(false);
    //增加按钮
    checkGroup->addButton(checkBox1);
    checkGroup->addButton(checkBox2);

    /***************************单选****************************/
    radioBtn1 = new QRadioButton("男");
    radioBtn2 = new QRadioButton("女");
    radioBtn1->resize(50,50);
    radioBtn2->resize(100,50);
    //按钮组
    radioGroup = new QButtonGroup(this);
    //设置互斥
    radioGroup->setExclusive(true);
    //增加按钮
    radioGroup->addButton(radioBtn1);
    radioGroup->addButton(radioBtn2);

    /**************************工具按钮****************************/
	QToolButton *toolBtn1 = new QToolButton;

    /***************************布局****************************/
	QVBoxLayout *vLay = new QVBoxLayout();
	QHBoxLayout *hLay = new QHBoxLayout();
	QVBoxLayout *vLay1 = new QVBoxLayout();
	QVBoxLayout *vLay2 = new QVBoxLayout();
	QVBoxLayout *vLay3 = new QVBoxLayout();
	QVBoxLayout *vLay4 = new QVBoxLayout();
    //添加控件
	vLay1->addWidget(pushBtn1);
	vLay1->addWidget(pushBtn2);
    vLay2->addWidget(radioBtn1);
    vLay2->addWidget(radioBtn2);
    vLay3->addWidget(checkBox1);
    vLay3->addWidget(checkBox2);
    //添加布局
	hLay->addLayout(vLay1);
	hLay->addLayout(vLay2);
	hLay->addLayout(vLay3);
	vLay->addLayout(hLay);
    textEdit = new QTextEdit("textEdit");
    vLay->addWidget(textEdit);
	//整体布局
    QWidget *pWidget = new QWidget(this);
    pWidget->setLayout(vLay);
    setCentralWidget(pWidget);

    /***************************连接信号和槽****************************/
    connect(pushBtn1, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(pushBtn2, SIGNAL(clicked()), this, SLOT(pushBtnSlot()));
    connect(radioGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(radioBtnSlot(QAbstractButton*)));
    connect(checkGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(checkBoxSlot(QAbstractButton*)));
    connect(checkBox1, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int)));
}

void mainWindow::pushBtnSlot()
{
    QPushButton *btn = (QPushButton *)sender();
    int id = btn->property("btnID").toInt();
    if (id==1)
    {
        pushBtn2->setIcon(QIcon(":/images/open.png"));
        pushBtn2->setText("有图片的按钮");
        textEdit->setText(tr("你点击了普通按钮"));
    }
    else
        textEdit->setText(tr("你点击了图片按钮"));
}

void mainWindow::checkBoxChanged(int state)
{
    if (state == Qt::Checked) // "选中"
    {
        textEdit->setText("Checked");
    }
    else if(state == Qt::PartiallyChecked) // "半选"
    {
        textEdit->setText("PartiallyChecked");
    }
    else // 未选中 - Qt::Unchecked
    {
        textEdit->setText("Unchecked");
    }
}

void mainWindow::checkBoxSlot(QAbstractButton *button)
{
    QString str;
    // 遍历按钮，获取选中状态
    QList<QAbstractButton*> list = checkGroup->buttons();
    foreach (QAbstractButton *pButton, list)
    {
        QString strStatus = pButton->isChecked() ? "Checked" : "Unchecked";
        str += QString("Button : %1 is %2").arg(pButton->text()).arg(strStatus);
        str += '\n';
    }
    qDebug() << str << endl;
    textEdit->setText(str);
}
void mainWindow::radioBtnSlot(QAbstractButton *button)
{
    // 当前点击的按钮
    QString str = QString("Clicked Button : %1").arg(button->text());
    textEdit->setText(str);
}
